package com.zsgeek.wisdchain.qutu.view.home;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieSyncManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.tools.LogUtil;
import com.zsgeek.wisdchain.qutu.view.webView.Html5WebView;

import butterknife.BindView;

public class BaseWebViewActivity extends BaseActivity {
    @BindView(R.id.top_layout)
    FrameLayout topLayout;
    @BindView(R.id.web_sbr)
    SeekBar mSeekBar;
    @BindView(R.id.rl_help_hasno)
    LinearLayout rl_help;

    private Html5WebView mWebView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_base_web_view;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
        } else {
            setTitle(intent.getStringExtra("title"));
            String path = intent.getStringExtra("path");
            if (path != null)
                initWebview(path);
        }

    }

    @Override
    protected void initDate(Bundle savedInstanceState) {

    }

    private void initWebview(String path) {
        mWebView = new Html5WebView(this, null);
        topLayout.addView(mWebView, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                LogUtil.i("shouldOverrideUrlLoading:" + view.getUrl());
                rl_help.setVisibility(View.GONE);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
//                closeProgressDialog();
            }

            @Override
            public void onPageStarted(WebView webView, String url, Bitmap bitmap) {
//                if (isOnUserVisible)
//                    showProgressDialog(getString(R.string.text_load_more));
                LogUtil.i("onPageStarted:" + url);
                rl_help.setVisibility(View.GONE);
                super.onPageStarted(webView, url, bitmap);
            }

            /*
             *此回调是拦截点击要跳转的url链接，并对请求的url链接做修改（添加删除字段）
             */
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
                return super.shouldInterceptRequest(webView, url);
            }

            /*
             *当加载出错的时候，显示下面的自定义页面
             */
            @Override
            public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
                rl_help.setVisibility(View.VISIBLE);
                super.onReceivedError(webView, errorCode, description, failingUrl);
            }

            /*
             *加载页面出错
             */
            @Override
            public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
                rl_help.setVisibility(View.VISIBLE);
                super.onReceivedError(webView, webResourceRequest, webResourceError);
            }

            @Override
            public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
                rl_help.setVisibility(View.VISIBLE);
                super.onReceivedSslError(webView, sslErrorHandler, sslError);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                rl_help.setVisibility(View.GONE);
                return false;
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webView, int newProgress) {
                if (newProgress == 100) {
                    mSeekBar.setVisibility(View.GONE);
                } else {
                    if (View.GONE == mSeekBar.getVisibility()) {
                        mSeekBar.setVisibility(View.VISIBLE);
                    }
                    mSeekBar.setProgress(newProgress);
                }
                super.onProgressChanged(webView, newProgress);
            }
        });

        WebSettings webSetting = mWebView.getSettings();
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        webSetting.setDatabasePath(this.getDir("databases", 0).getPath());
        webSetting.setGeolocationDatabasePath(this.getDir("geolocation", 0).getPath());
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);

        mWebView.loadUrl(path);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().sync();

    }

}
